package com.takwolf.android.loadmore;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class UserAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<User> userList;

    public UserAdapter(Context context, List<User> userList) {
        inflater = LayoutInflater.from(context);
        this.userList = userList;
    }

    @Override
    public int getCount() {
        return userList == null ? 0 : userList.size();
    }

    @Override
    public Object getItem(int position) {
        return userList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.activity_main_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        User user = userList.get(position);
        holder.tvTitle.setText(user.getName());
        holder.tvContent.setText(user.getEmail());
        holder.btnNext.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                context.startActivity(new Intent(context, NextActivity.class));
            }

        });
        return convertView;
    }

    private static class ViewHolder {

        public final TextView tvTitle;
        public final TextView tvContent;
        public final Button btnNext;

        public ViewHolder(View convertView) {
            tvTitle = (TextView) convertView.findViewById(R.id.main_item_tv_title);
            tvContent = (TextView) convertView.findViewById(R.id.main_item_tv_content);
            btnNext = (Button) convertView.findViewById(R.id.main_item_btn_next);
        }

    }

}
